<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <div>
      <div style="margin: 0 0 10px;overflow: hidden">
        <div style="float: left;">
          <router-link :to="{name: 'casesEdit'}">
            <el-button type="primary" plain>添加</el-button>
          </router-link>
          <el-button type="danger" plain @click="delObjs()">删除</el-button>
          <el-button type="danger" plain @click="testCases()">测试</el-button>
          <el-button type="primary" :disabled="!is_show" plain @click="inputFunc()">查询</el-button>
          <el-switch style="padding-left: 15px" v-model="is_show">
          </el-switch>
        </div>
      </div>

      <div v-if="is_show">

        <!--负责人-->
        <div class="input-data">
          <el-select
            v-model="optionsTestPersonId"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="负责人用户名"
            :remote-method="getTestPersonInfo"
            :loading="loading">
            <el-option
              v-for="(item,index) in test_person_info"
              :key="index"
              :label="item.username"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <!--项目-->
        <div class="input-data">
          <el-select
            v-model="optionsProjectId"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入项目名称"
            :remote-method="getProjectInfo"
            :loading="loading">
            <el-option
              v-for="(item,index) in project_info"
              :key="index"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <!--模块-->
        <div class="input-data">
          <el-select
            v-model="optionsModuleId"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入模块"
            :remote-method="getModuleInfo"
            :loading="loading">
            <el-option
              v-for="(item,index) in module_info"
              :key="index"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <div class="input-data">
          <el-select v-model="describe" placeholder="测试结果">
            <el-option
              v-for="(item,index) in optionsDescribe"
              :key="index"
              :label="item.value"
              :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div class="input-data">
          <el-select v-model="property" placeholder="用例属性">
            <el-option
              v-for="(item,index) in optionsProperty"
              :key="index"
              :label="item.value"
              :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div class="input-data">
          <el-select v-model="status" placeholder="请选择类型">
            <el-option
              v-for="(item,index) in optionsCommon"
              :key="index"
              :label="item.value"
              :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div class="input-data">
          <el-input placeholder="请输入用例标题" v-model="inputObjName"></el-input>
        </div>
      </div>

      <div>
        <!--内容组件-->
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange">

          <el-table-column
            type="selection"
            width="55">
          </el-table-column>

          <el-table-column
            prop="id"
            label="ID"
            width="50">
          </el-table-column>

          <el-table-column
            prop="name"
            label="用例标题"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.name">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="所属项目"
            width="180">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>项目ID: {{ scope.row.project_info.id }}</p>
                <p>项目名称: {{ scope.row.project_info.name }}</p>
                <p>项目描述: {{ scope.row.project_info.describe }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.project_info.name }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            label="所属模块"
            width="180">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>模块ID: {{ scope.row.module_info.id }}</p>
                <p>模块名称: {{ scope.row.module_info.name }}</p>
                <p>开发人: {{ scope.row.module_info.developer }}</p>
                <p>测试人: {{ scope.row.module_info.testers }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.module_info.name }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            label="用例负责人"
            width="180">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>负责人ID: {{ scope.row.test_person_info.id }}</p>
                <p>负责人名称: {{ scope.row.test_person_info.username }}</p>
                <p>负责人描述: {{ scope.row.test_person_info.name }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.test_person_info.username }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            prop="precondition"
            label="预置条件"
            width="300">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.precondition">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="test_data"
            label="测试数据"
            width="300">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.test_data">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="test_steps"
            label="测试步骤"
            width="300">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.test_steps">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="expected_results"
            label="预期结果"
            width="300">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.expected_results">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="warehouse_data"
            label="数据仓库"
            width="200">
            <template slot-scope="scope">
              <div v-show="scope.row.warehouse_data">
                <el-button type="text" size="small" @click="downTemplate(scope.row.warehouse_data)">下载</el-button>
                <el-link type="success" :href="getUrl(scope.row.warehouse_data)" target="_blank">查看</el-link>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="create_time"
            label="创建日期"
            width="200">
          </el-table-column>

          <el-table-column
            prop="update_time"
            label="更新日期"
            width="200">
          </el-table-column>

          <el-table-column
            prop="execution_time"
            label="执行时间"
            width="200">
          </el-table-column>

          <el-table-column
            prop="consume_time"
            label="用例耗时"
            width="200">
          </el-table-column>

          <el-table-column
            fixed="right"
            prop="exe_number"
            label="执行次数"
            width="100">
          </el-table-column>

          <el-table-column
            prop="cases_count"
            label="所用步数"
            width="100">
          </el-table-column>

          <el-table-column fixed="right" prop="status" label="用例属性" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.property" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsProperty"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column fixed="right" prop="status" label="重要级别" width="90">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.level" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsLevel"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column fixed="right" prop="test_status" label="测试结果" width="130">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.test_status" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsTestStatus"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column fixed="right" prop="status" label="状态" width="100">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.status" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsCommon"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            width="150">
            <template slot-scope="scope">
              <router-link :to="{name: 'casesDetail', query: { cases_id: scope.row.id }}">
                <el-button type="text" size="small">查看步骤</el-button>
              </router-link>
              <router-link :to="{name: 'casesEdit', query: { cases_id: scope.row.id }}">
                <el-button type="text" size="small">编辑</el-button>
              </router-link>
              <el-button type="text" size="small" @click="delObjs(scope.row.id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页组件-->
      <div class="block paging">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          prev-text="上一页"
          next-text="下一页"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Cases',
    data() {
      return {
        // 搜索功能实现
        is_show: false,
        status: '',  // 状态
        property: '',  // 用例属性
        describe: '',  // 测试结果
        // 需要搜索的项目名称
        inputObjName: '',  // 搜索功能实现的字符串
        // choices设置
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsProperty: [],  // 用例属性
        optionsLevel: [],  // 用例级别
        optionsTestStatus: [],  // 测试结果
        // 分页设置
        currentPage: 1, //当前页码
        pageSize: 10,    //    每页的条数
        pageSizes: [10, 20, 30, 40],  // 可选每页显示条数
        dataCount: 0,  // 数据总条数

        project_info: [],// 项目列表，获取1000条项目数据
        test_person_info: [],  // 用例负责人
        module_info: [],  // 用例所属模块

        // 编辑和添加选中的数据id
        loading: false,
        optionsProjectId: [],
        optionsTestPersonId: [],
        optionsModuleId: [],

        // 数据设置
        ListDataURL: this.$http.casesURL,  // 表格列表数据获取
        objID: 'cases_id',
        tableData: [],  // 阿贾克斯获取的初始数据
        multipleSelection: [],  // 不能删，我也不知道是干嘛用的，删了会报错
      }
    },
    created() {
      // 发送啊假克丝请求
      this.objList();// 阿贾克斯获取的初始数据
      this.optionsCommon = this.getChoices.common;  // 获取标准CHOICES
      this.optionsProperty = this.getChoices.cases.property;  // 用例属性
      this.optionsLevel = this.getChoices.cases.level;  // 用例级别
      this.optionsTestStatus = this.getChoices.test_status;  // 测试结果
    },

    methods: {
      // 下载文件
      downTemplate(url) {
        this.$myTool.downTemplate(this, url)
      },
      // 跳转查看url
      getUrl(url) {
        return this.$http.HOST + url
      },

      handleClick(row) {
        console.log(row);
      },
      // 修改每页显示条数
      handleSizeChange(val) {
        this.pageSize = val;  // 修改每页显示条数
        this.currentPage = 1;  // 修改页码
      },
      // 修改页码
      handleCurrentChange(val) {
        this.currentPage = val;  // 修改页码
      },

      // 发送啊假克丝请求 得到表格的原始数据
      objList() {
        this.$myTool.objList(this)
      },

      // 删除函数，调用了删除接口
      delObjs(objId) {
        this.$myTool.delObjs(objId, this)
      },

      testCases(){
        let test_ids = [];
        this.multipleSelection.forEach(item=>{
          test_ids.push(item.id);
        });
        this.$http.getApiFunc(this.$http.addApiStatus)(this.$http.testCasesURL,{'id':test_ids})
          .then(res=>{
            this.$alert(res.data.msg, '提示', {
              confirmButtonText: '确定'
            });
            this.objList();
          })
          .catch(err=>{
            console.log(err)
          })
      },

      //  搜索的函数
      inputFunc() {
        this.$myTool.inputFunc(this)
      },

      getInputData() {
        return {
          cases_project: this.optionsProjectId[0],
          cases_module: this.optionsModuleId[0],
          cases_test_person: this.optionsTestPersonId[0],

          cases_status: this.status,
          cases_property: this.property,
          cases_describe: this.describe,

          inputObjName: this.inputObjName
        }
      },

      // 不能删
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },

      // 用例所属人
      getTestPersonInfo(query) {
        this.$myTool.getObjList(this, this.$http.accountURL, this.test_person_info, query)
      },

      // 项目信息搜索
      getProjectInfo(query) {
        this.$myTool.getObjList(this, this.$http.projectURL, this.project_info, query)
      },
      // 模块
      getModuleInfo(query) {
        let queryData = {
          inputObjName: query,
          module_project: this.optionsProjectId[0]
        };
        this.$myTool.getObjList(this, this.$http.moduleURL, this.module_info, queryData)
      },

    },

    watch: {

      'pageSize': function (value) {
        this.objList();
      },
      'currentPage': function (value) {
        this.objList();
      },

      optionsProjectId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
      },
      optionsTestPersonId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
      },

      optionsModuleId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
      },
    },

    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>

<style scoped>
  .paging {
    padding-top: 20px;
  }

  .input-data {
    float: left;
    width: 200px;
    padding: 5px 5px;
  }

  .mytable {
    /*width: 120px;*/
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>
